﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursing
{
    public partial class AddPICC : Form
    {
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        string Id;
        public AddPICC(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            this.LoginUser = loginUser;
            this.Id = id;
            BindInfo(id);
        }
        public AddPICC(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;
            BindInfo(this.Id);
        }
        public AddPICC(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;
            BindInfo(this.Id);
        }
        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text.Trim()))
                dateriqi.DateTime = DateTime.Now;
            PICCWashPipe picc = new PICCWashPipe();
            picc.FormId = (int)FormType.PICCWashPipeNursing;
            picc.FormName = EnumHelper.GetDescription(FormType.PICCWashPipeNursing);
            picc.RiQi = dateriqi.Text;
            picc.WaiLuChangDu = textwailu.Text;
            picc.ShangTunWei = texttunwei.Text;
            picc.GuanLuShiFouChangTong = comguanlu.Text;
            picc.GengHuanGanSuMao = textgangsumao.Text;
            picc.GengHuanFuTie = textfutie.Text;
            picc.ChongGuanYeTi = textchongguan.Checked ? "1" : "0";
            picc.FengGuan = textfeiguan.Checked ? "1" : "0";
            picc.HuLiCuoShi = hulicuoshi.Text;
            if (string.IsNullOrEmpty(this.Id))
                picc.QianMing = this.LoginUser.UserName;
            else
                picc.QianMing = textqianming.Text;
            //穿刺部位情况
            picc.ChuangCiBuWeiQingKuang = (checkZhengchang.Checked ? 1 : 0) + "#" + (checkShenxue.Checked ? 1 : 0) + "#" +
                (checkShenye.Checked ? 1 : 0) + "#" + (checkHongzhon.Checked ? 1 : 0) + "#" + (checkQita.Checked ? 1 : 0);
            if (checkQita.Checked)
                picc.ChuangCiBuWeiQingKuangWenBenKuang = textqita.Text;
            else
                picc.ChuangCiBuWeiQingKuangWenBenKuang = string.Empty;
            record.NursingFormData = XmlUtil.ObjectToXML(picc, Encoding.UTF8);
            record.FormId = picc.FormId;//表单ID
            record.FormName = picc.FormName;//表单NAME
            record.PatientId = PatientInfo.PATIENT_ID;//住院号
            record.VisitId = PatientInfo.VISIT_ID;//住院次数
            record.PatientName = PatientInfo.PATIENT_NAME;//患者名字
            record.BedNo = PatientInfo.BED_NO;//床号
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;//科室id
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;//科室名称
            record.AreaId = PatientInfo.NURSE_CELL_CODE;//病区编号
            record.AreaName = PatientInfo.NURSE_CELL_NAME;//病区名称
            record.AssessmentDate = dateriqi.DateTime;//日期
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        private void BindInfo(string id)
        {
            dateriqi.DateTime = DateTime.Now;   //显示当前日期
            textqianming.Text = LoginUser.UserName;
            if (string.IsNullOrEmpty(id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            PICCWashPipe ps = new PICCWashPipe();
            ps = XmlUtil.XMLToObject<PICCWashPipe>(NursingFormData, Encoding.UTF8);
            dateriqi.Text = ps.RiQi;
            textwailu.Text = ps.WaiLuChangDu;
            texttunwei.Text = ps.ShangTunWei;
            comguanlu.Text = ps.GuanLuShiFouChangTong;
            textgangsumao.Text = ps.GengHuanGanSuMao;
            textfutie.Text = ps.GengHuanFuTie;
            textchongguan.Checked = ps.ChongGuanYeTi.Equals("1");
            textfeiguan.Checked = ps.FengGuan.Equals("1");
            hulicuoshi.Text = ps.HuLiCuoShi;
            textqianming.Text = ps.QianMing;
            var ChuanCi = ps.ChuangCiBuWeiQingKuang.Split('#'); //穿刺部位情况
            checkZhengchang.Checked = ChuanCi[0].Equals("1");
            checkShenxue.Checked = ChuanCi[1].Equals("1");
            checkShenye.Checked = ChuanCi[2].Equals("1");
            checkHongzhon.Checked = ChuanCi[3].Equals("1");
            checkQita.Checked = ChuanCi[4].Equals("1");
            textqita.Text = ps.ChuangCiBuWeiQingKuangWenBenKuang;
            textqita.Visible = checkQita.Checked;
        }

        private void checkQita_CheckedChanged(object sender, EventArgs e)
        {
            textqita.Visible = checkQita.Checked;
        }
    }
}
